package br.com.jmed.control;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.inject.Inject;

import br.com.jmed.model.Medico;
import br.com.jmed.service.MedicoService;

@ManagedBean(name = "medicoControl")
@SessionScoped
public class MedicoControl extends BaseControl implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@EJB
	private MedicoService medicoService;

	@Inject
	private Medico medico;
	private Medico medicoSelect;
	private List<Medico> listMedico;

	public String salvarCadastroMedico() throws IOException {
		medico.setUsuarioInclusao(getUsuario());
		medico.setDataInclusao(new Date());
		medicoService.salvarMedico(medico);
		this.medico = new Medico();
		return "/pages/medico/medicoBusca.xhtml";
	}

	public String novoCadastroMedico() throws IOException {
		this.medico = new Medico();
		this.listMedico = new ArrayList<Medico>();
		return "/pages/medico/medicoIncluir.xhtml";
	}

	public String alterarCadastroMedico() throws IOException {
		listMedico = new ArrayList<Medico>();
		medicoSelect.setUsuarioAlteracao(getUsuario());
		medicoSelect.setDataAlteracao(new Date());
		medicoService.atualizarMedico(medicoSelect);
		medicoSelect = new Medico();
		return "/pages/medico/medicoBusca.xhtml";
	}

	public String abrirAlterarMedico(Medico medico) {
		this.medicoSelect = medicoService.findMedicoByCod(medico.getCodigo());
		return "/pages/medico/medicoAlterar.xhtml";
	}

	public void pesquisarMedico() {
		listMedico = new ArrayList<Medico>();
		if (medico.getCodigo() != 0) {
			medico = medicoService.findMedicoByCod(medico.getCodigo());
			listMedico.add(medico);
		} else if (!medico.getNome().trim().equals("")) {
			listMedico = medicoService.findMedicoByNome(medico.getNome());
			createFormMessage("form-med-busc", "" + listMedico.size());
		} else {
			listMedico = medicoService.findAllMedicos();
		}
		medico = new Medico();
	}

	public List<String> pesquisarAutoCompMedico(String prefix) {
		List<String> toReturn = new ArrayList<String>();
		listMedico = new ArrayList<Medico>();
		listMedico = medicoService.findAllMedicos();
		for (Medico m : listMedico) {
			if (m.getNome().startsWith(prefix)) {
				toReturn.add(m.getNome());
			}
		}
		return toReturn;
	}

	public MedicoService getMedicoService() {
		return medicoService;
	}

	public void setMedicoService(MedicoService medicoService) {
		this.medicoService = medicoService;
	}

	public Medico getMedico() {
		return medico;
	}

	public void setMedico(Medico medico) {
		this.medico = medico;
	}

	public List<Medico> getListMedico() {
		return listMedico;
	}

	public void setListMedico(List<Medico> listMedico) {
		this.listMedico = listMedico;
	}

	public Medico getMedicoSelect() {
		return medicoSelect;
	}

	public void setMedicoSelect(Medico medicoSelect) {
		this.medicoSelect = medicoSelect;
	}

}
